﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string source = string.Empty, destination = string.Empty;
        bool flag = false;
        public MainWindow()
        {
            InitializeComponent();
            //this.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;        
            //this.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "DashBoard   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            //else
            //{
            //    App.Inventory_DashBoard = new TabItem();
            //    App.Inventory_DashBoard.Header = "DashBoard   ";

            //    Grid grdDashBoard = new Grid();
            //    App.Inventory_DashBoard.Content = grdDashBoard;

            //    DashBoard DashBoardMasterObj = new DashBoard();
            //    grdDashBoard.Children.Add(DashBoardMasterObj);

            //    tabMainLayout.Items.Add(App.Inventory_DashBoard);
            //    App.Inventory_DashBoard.Focus();
            //}
        }

        private void TabItemClose_Click(Object sender, RoutedEventArgs args)
        {
            TabControl tab;
            TabItem item;

            if (args.Source.GetType().ToString() == "System.Windows.Controls.TabItem")
            {
                tab = ((TabControl)sender);
                item = ((TabItem)args.Source);

                if (item != null)
                {
                    if ((item.Header.ToString() == "Employee_SearchEmployeeInformation   ") && ((EmployeeInformation.NavigateEmpNo != null) || (TemporaryEmployeeInformation.NavigateEmpNo != null))) ;
                    {
                        EmployeeInformation.NavigateEmpNo = null;
                        TemporaryEmployeeInformation.NavigateEmpNo = null;
                    }
                    tab.Items.Remove(item);
                }
            }
        }

        private void Employee_EmployeeInformation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //source = "Employee_EmployeeInformation   ";
            flag = false;
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_EmployeeInformation   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Employee_EmployeeInformation = new TabItem();
                App.Employee_EmployeeInformation.Header = "Employee_EmployeeInformation   ";

                Grid grdMaster = new Grid();
                App.Employee_EmployeeInformation.Content = grdMaster;

                EmployeeInformation EmpMasterObj = new EmployeeInformation();
                grdMaster.Children.Add(EmpMasterObj);

                tabMainLayout.Items.Add(App.Employee_EmployeeInformation);
                App.Employee_EmployeeInformation.Focus();
            }
        }

        private void Employee_TempEmployeeInformation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            //source = "Employee_TemporaryEmployeeInformation   ";
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_TemporaryEmployeeInformation   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Employee_TempEmployeeInformation = new TabItem();
                App.Employee_TempEmployeeInformation.Header = "Employee_TemporaryEmployeeInformation   ";

                Grid grdMaster = new Grid();
                App.Employee_TempEmployeeInformation.Content = grdMaster;

                TemporaryEmployeeInformation EmpMasterObj = new TemporaryEmployeeInformation();
                grdMaster.Children.Add(EmpMasterObj);

                tabMainLayout.Items.Add(App.Employee_TempEmployeeInformation);
                App.Employee_TempEmployeeInformation.Focus();
            }
        }

        private void Employee_SearchEmployeeInformation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            source = "Employee_SearchEmployeeInformation   ";
            flag = false;
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_SearchEmployeeInformation   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Employee_SearchEmployeeInformation = new TabItem();
                App.Employee_SearchEmployeeInformation.Header = "Employee_SearchEmployeeInformation   ";

                Grid grdMaster = new Grid();
                App.Employee_SearchEmployeeInformation.Content = grdMaster;

                SearchEmployeeInformation EmpMasterObj = new SearchEmployeeInformation();
                grdMaster.Children.Add(EmpMasterObj);

                tabMainLayout.Items.Add(App.Employee_SearchEmployeeInformation);
                App.Employee_SearchEmployeeInformation.Focus();
            }
        }

        private void Organisation_SectionManagement_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            //source = "Organisation_SectionManagement   ";
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Organisation_SectionManagement   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Organisation_SectionManagement = new TabItem();
                App.Organisation_SectionManagement.Header = "Organisation_SectionManagement   ";

                Grid grdMaster = new Grid();
                App.Organisation_SectionManagement.Content = grdMaster;

                SectionManagement OrgMasterObj = new SectionManagement();
                grdMaster.Children.Add(OrgMasterObj);

                tabMainLayout.Items.Add(App.Organisation_SectionManagement);
                App.Organisation_SectionManagement.Focus();
            }
        }

        private void Organisation_TaskManagement_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            //source = "Organisation_TaskManagement   ";
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Organisation_TaskManagement   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Organisation_TaskManagement = new TabItem();
                App.Organisation_TaskManagement.Header = "Organisation_TaskManagement   ";

                Grid grdMaster = new Grid();
                App.Organisation_TaskManagement.Content = grdMaster;

                TaskManagement OrgMasterObj = new TaskManagement();
                grdMaster.Children.Add(OrgMasterObj);

                tabMainLayout.Items.Add(App.Organisation_TaskManagement);
                App.Organisation_TaskManagement.Focus();
            }
        }

        private void Organisation_EmployeeTaskMapping_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Organisation_EmployeeTaskMapping   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Organisation_EmployeeTaskMapping = new TabItem();
                App.Organisation_EmployeeTaskMapping.Header = "Organisation_EmployeeTaskMapping   ";

                Grid grdMaster = new Grid();
                App.Organisation_EmployeeTaskMapping.Content = grdMaster;

                EmployeeTaskMapping OrgMasterObj = new EmployeeTaskMapping();
                grdMaster.Children.Add(OrgMasterObj);

                tabMainLayout.Items.Add(App.Organisation_EmployeeTaskMapping);
                App.Organisation_EmployeeTaskMapping.Focus();
            }
        }

        private void Organisation_EmployeeAttendence_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Organisation_EmployeeAttendence   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Organisation_EmployeeAttendence = new TabItem();
                App.Organisation_EmployeeAttendence.Header = "Organisation_EmployeeAttendence   ";

                Grid grdMaster = new Grid();
                App.Organisation_EmployeeAttendence.Content = grdMaster;

                EmployeeAttendence OrgMasterObj = new EmployeeAttendence();
                grdMaster.Children.Add(OrgMasterObj);

                tabMainLayout.Items.Add(App.Organisation_EmployeeAttendence);
                App.Organisation_EmployeeAttendence.Focus();
            }
        }

        private void Organisation_EmployeeAttendenceReport_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            flag = false;
            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Organisation_EmployeeAttendenceReport   ").FirstOrDefault();

            if (matchingItem != null)
                tabMainLayout.SelectedItem = matchingItem;
            else
            {
                App.Organisation_EmployeeAttendenceReport = new TabItem();
                App.Organisation_EmployeeAttendenceReport.Header = "Organisation_EmployeeAttendenceReport   ";

                Grid grdMaster = new Grid();
                App.Organisation_EmployeeAttendenceReport.Content = grdMaster;

                EmployeeAttendenceReport OrgMasterObj = new EmployeeAttendenceReport();
                grdMaster.Children.Add(OrgMasterObj);

                tabMainLayout.Items.Add(App.Organisation_EmployeeAttendenceReport);
                App.Organisation_EmployeeAttendenceReport.Focus();
            }
        }

        private void tabMainLayout_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabMainLayout.SelectedIndex != -1)
            {
                destination = ((TabItem)(tabMainLayout.SelectedItem)).Header.ToString();
            }
            if ((EmployeeInformation.NavigateEmpNo != null) || (TemporaryEmployeeInformation.NavigateEmpNo != null))
            {
                if ((source == "Employee_SearchEmployeeInformation   ") && (destination != null) && (source != destination) && (EmployeeInformation.NavigateEmpNo != null) || (TemporaryEmployeeInformation.NavigateEmpNo != null))
                {
                    if (flag == false)
                    {
                        flag = true;
                        var res = MessageBox.Show("Do you want To discard the Changes", "Information", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (res == MessageBoxResult.Yes)
                        {
                            if (EmployeeInformation.NavigateEmpNo != null)
                            {
                                var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_EmployeeInformation   ").FirstOrDefault();

                                if (matchingItem != null)
                                    tabMainLayout.Items.Remove(matchingItem);

                                matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_SearchEmployeeInformation   ").FirstOrDefault();

                                if (matchingItem != null)
                                    tabMainLayout.Items.Remove(matchingItem);
                            }

                            if (TemporaryEmployeeInformation.NavigateEmpNo != null)
                            {
                                var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_TemporaryEmployeeInformation   ").FirstOrDefault();

                                if (matchingItem != null)
                                    tabMainLayout.Items.Remove(matchingItem);

                                matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_SearchEmployeeInformation   ").FirstOrDefault();

                                if (matchingItem != null)
                                    tabMainLayout.Items.Remove(matchingItem);
                            }
                            EmployeeInformation.NavigateEmpNo = null;
                            TemporaryEmployeeInformation.NavigateEmpNo = null;
                            source = string.Empty;
                        }
                        else if (res == MessageBoxResult.No)
                        {
                            var matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == destination).FirstOrDefault();

                            if (matchingItem != null)
                                tabMainLayout.Items.Remove(matchingItem);

                            matchingItem = tabMainLayout.Items.Cast<TabItem>().Where(item => item.Header.ToString() == "Employee_SearchEmployeeInformation   ").FirstOrDefault();

                            if (matchingItem != null)
                                tabMainLayout.SelectedItem = matchingItem;
                        }
                    }
                }
            }
        }
    }   


}